function [EC] = APESERDStoHDF5(filename,setdisp,varargin)
%% APE SERDS to HDF5
% APESERDStoHDF5(filename,setdisp) 
%
% Converts an SERDS file constructed by MAKESERDS to HDF5 fileformat
% readable by other HDF5 enabled packages.
%
% INPUT     filename    :   filename to process (must contain SERDS data).
%                           Will also be used as output filename with the
%                           extension '.hdf5'
%           setdisp     :   0 suppresses output of HDF5 datastructure to
%                             commandline after succesful processing
%
% OUTPUT    EC          :   error code (0 = success, 1 = error)
%

%  (c) F. Dworkowski, 2012
%      Version 1.0, 2012-08-03

%% ERROR Checking
if nargin < 1 || nargin > 2
    help APESERDStoHDF5
    EC = 1;
    return
elseif nargin < 2
    setdisp = 0;
end

% load data
try load([filename,'.mat']);
catch ME
    EC = 1;
    rethrow(ME)
end

% generate output filename and check for existense
filenameout = [filename,'.hdf5'];
if exist(filenameout,'file')
    EC = 1;
    warning(['ERROR: ',filenameout,' already exists!'])
    return
end

%% Datastructure
%  Start the construction of the HDF5 file

%% SERDSdata
%  look if 'SERDSdata' structure exists in file and if yes process it into
%  HDF5 datastructre
if exist('SERDSdata','var')
    h5create(filenameout,'/SERDSdata/datax',size(SERDSdata.datax))
        h5write(filenameout,'/SERDSdata/datax',SERDSdata.datax)
    h5create(filenameout,'/SERDSdata/datay',size(SERDSdata.datay))
        h5write(filenameout,'/SERDSdata/datay',SERDSdata.datay)
% -------------------------------------------------------------------------        
% % uncomment to store X and recony in one column array for easy use with
% % HDF5view etc.
%         reconstdata(:,1) = SERDSdata.datax;
%         reconstdata(:,2) = SERDSdata.reconsty;
%     h5create(filenameout,'/SERDSdata/recondatay',size(reconstdata'))
%         h5write(filenameout,'/SERDSdata/recondatay',reconstdata')
% -------------------------------------------------------------------------
% uncomment to store in regular single vector APE format
    h5create(filenameout,'/SERDSdata/recondatay',size(SERDSdata.reconsty))
        h5write(filenameout,'/SERDSdata/recondatay',SERDSdata.reconsty)
% -------------------------------------------------------------------------
    % Attributes
    h5writeatt(filenameout,'/SERDSdata','setofftype',SERDSdata.setofftype)
    h5writeatt(filenameout,'/SERDSdata','setoff',SERDSdata.setoff)
else
    warning(['No SERDSdata found in "',filename,'.mat"!'])
end

%% DATASET1
%  look if 'dataset1' structure exists in file and if yes process it into
%  HDF5 datastructre
if exist('dataset1','var')
    h5create(filenameout,'/dataset1/datay',size(dataset1.datay))
        h5write(filenameout,'/dataset1/datay',dataset1.datay)
    h5create(filenameout,'/dataset1/datax',size(dataset1.datax))
        h5write(filenameout,'/dataset1/datax',dataset1.datax)
    % Convert FOOTER structure to HDF5 Attributes
        fieldn = fieldnames(dataset1.footer);
        newfooter = struct2cell(dataset1.footer);
        for i=1:length(fieldn)
            h5writeatt(filenameout,'/dataset1',cell2mat(fieldn(i)),cell2mat(newfooter(i)));
            %disp([cell2mat(fieldn(i)),' = ',cell2mat(newfooter(i))])
        end
else
    warning(['No dataset1 found in "',filename,'.mat"!'])
end

%% DATASET2
%  look if 'dataset2' structure exists in file and if yes process it into
%  HDF5 datastructre
if exist('dataset2','var')
    h5create(filenameout,'/dataset2/datax',size(dataset2.datax))
        h5write(filenameout,'/dataset2/datax',dataset2.datax)
    h5create(filenameout,'/dataset2/datay',size(dataset2.datay))
        h5write(filenameout,'/dataset2/datay',dataset2.datay)
    % Convert FOOTER structure to HDF5 Attributes
        fieldn = fieldnames(dataset2.footer);
        newfooter = struct2cell(dataset2.footer);
        for i=1:length(fieldn)
            h5writeatt(filenameout,'/dataset2',cell2mat(fieldn(i)),cell2mat(newfooter(i)));
            %disp([cell2mat(fieldn(i)),' = ',cell2mat(newfooter(i))])
        end
else
    warning(['No dataset2 found in "',filename,'.mat"!'])
end
    

%% Display file
if setdisp ~= 0
    h5disp(filenameout)
end

EC = 0; %indicate successfull operation